var doit;
window.onresize = function(){
	clearTimeout(doit);
	doit = setTimeout(resizeGlobal, 100);
};

resizeGlobal();
function resizeGlobal(){
	checkHeaderBarFixed();
}

// LazyLoad
var bLazy = new Blazy({
	container: ".b-lazy"
});

// Webfont
GetWebFont(jQuery(".webFont60S"), "汉仪旗黑-60S");
GetWebFont(jQuery(".webFont50S"), "汉仪旗黑-50S");
function GetWebFont(fontSelector, fontName){
	var webFontText = "";
	fontSelector.each(function(index){
		webFontText += jQuery(this).html().match(/[\u4E00-\u9FFF\u3400-\u4DFF\uF900-\uFAFF]+/g);//jQuery(this).html().match(/[^A-Za-z 0-9 \.,\?""!@#\$%\^&\*\(\)-_=\+;:<>\/\\\|\}\{\[\]`~]*/g).join('');
	});
	if(webFontText != 'null' && webFontText != null && webFontText.length > 0){
		var jsonWebFontBuildParamObj = { 'FontName': fontName, 
									 'ChaStr': webFontText, 
									 'UserGuid': "430196D4-7A74-4E9A-B772-E712200F60DD"}
		jQuery.ajax({
			url: "http://www.hanyifont.cn:1000/webservicewebfontbuilder.asmx/WebFontBuild",
			type:'POST',
			data: { jsonWebFontItem: JSON.stringify(jsonWebFontBuildParamObj) },
			dataType: "xml"
		}).done(function(xml) {	
			jQuery(xml).find('string').each(function(){
		        var dataJson = jQuery.parseJSON(jQuery(this).text());
				jQuery('head').append(dataJson["RefLink"]);
				fontSelector.css({
					fontFamily: '"SF UI Text", Helvetica, ' + dataJson["FontFamily"]
				});
		    });
		});
	}	
}

// Infinite Scroll
function loadArticle(offset, posts_per_page, page, json, callback) {
    jQuery.ajax({
        url: "/wp-admin/admin-ajax.php",
        type:'POST',
        data: { 
        	"action": "infinite_scroll",
        	"loop_file": "infiniteloop",
        	"posts_per_page": posts_per_page,
        	"offset": offset,
        	"page": page,
        	"json": json
    	}, success: callback
    });
}

function caluPostHeight(selector){
	var col_height=0;
	var col_elem;

	jQuery(selector).find(".postItem, .memberItem").height("auto");
	jQuery(selector).find(".postItem, .memberItem").each(function () {
	    $this = jQuery(this);
	    if($this.outerHeight() > col_height) {
	        col_elem=this;
	        col_height=$this.outerHeight();
	    }
	});
	jQuery(selector).find(".postItem, .memberItem").height(col_height);
}

// Menu Hover
jQuery(".sub-menu").hover(
	function() {
		jQuery("#menu-main-menu > li:nth-child(1) > a").toggleClass('changed');
	}, 
	function() {
		jQuery("#menu-main-menu > li:nth-child(1) > a").toggleClass('changed');
	}
);

// Search
jQuery(".site-header > .container > .search-area").hover(function() {
	jQuery(".search-area > .bg-search, #searchform > #s").fadeIn(0);
	
}, function() {
	if(!jQuery('#searchform > input[type="text"]').is(':focus')) {
		jQuery(".search-area > .bg-search, #searchform > #s").fadeOut(0);
	}
});
jQuery('#searchform > input[type="text"]').focus(function() {
	jQuery(".search-area > .bg-search, #site-navigation, .site-header > .container > .btn-followus").fadeOut(300);
	jQuery(".site-header > .container > .btn-search, .search-area > .mask").fadeIn(300);
});

jQuery('#searchform > input[type="text"]').blur(function() {
	jQuery("#site-navigation, .site-header > .container > .btn-followus").fadeIn(300);
	jQuery(".site-header > .container > .btn-search, .search-area > .mask").fadeOut(300);
	jQuery("#searchform > #s").fadeOut(0);
});

jQuery(".site-header > .container > .btn-search").click(function() {
	jQuery('#searchform').submit();
});
jQuery(".site-navigation-mobile > .navigation-bar > .icon-search").click(function() {
	jQuery(".site-navigation-mobile .popup-search").fadeIn(300);
});
jQuery(".site-navigation-mobile .popup-search .btn-close").click(function() {
	jQuery(".site-navigation-mobile .popup-search").fadeOut(300);
});
jQuery(".site-navigation-mobile #searchform-mobile > .icon-search").click(function() {
	jQuery('#searchform-mobile').submit();
});

// Fixed Header
jQuery(window).scroll(function() {
	if(jQuery(window).width() >= 1024) {
		checkHeaderBarFixed();
	}
});
function checkHeaderBarFixed(){
	if(jQuery(window).scrollTop() > 110) {
		if(btnFollowUs){
			jQuery(".site-menu").css({top: 370});
		}else{
			jQuery(".site-followus").css({"height": 0});
		}
		jQuery(".site-menu").removeClass("absolute").addClass("fixed");
	}else{
		if(btnFollowUs){
			jQuery(".site-menu").css({top: ""});
		}else{
			jQuery(".site-followus").css({"height": 10});
		}
		jQuery(".site-menu").removeClass("fixed").addClass("absolute");
	}
}

// Fowllow us
var btnFollowUs = false;
jQuery(".site-menu > .btn-followus").click(function(){
	if(btnFollowUs){
		jQuery(".site-followus .btn-close").trigger("click");
	}else{
		jQuery(".site-followus").animate({
			height: "+=370"
		}, 300);
		jQuery(".site-menu.fixed").animate({
			top: "+=370"
		}, 300);
		jQuery("#page").animate({
			marginTop: "+=370"
		}, 300);
		jQuery(".site-menu > .btn-followus").addClass("on");
		btnFollowUs = true;
	}
});
jQuery(".site-menu-mobile > .btn-followus").click(function(){
	jQuery(".site-followus").toggleClass("popup");
	jQuery("body").css({
		overflow: "hidden"
	});
});
jQuery(".site-followus .btn-close").click(function(){
	jQuery(".site-menu > .btn-followus").removeClass("on");
	if(jQuery(window).width() > 1024) {
		jQuery(".site-followus")
			.animate({
			height: "-=370"
		}, 300);
		jQuery(".site-menu.fixed").animate({
			top: "-=370"
		}, 300, function(){
			jQuery(".site-menu").css({
				top: ""
			});
		});
		jQuery("#page").animate({
			marginTop: "-=370"
		}, 300);
	}else{
		jQuery(".site-followus")
			.removeClass("popup")
			.animate({
			height: "10"
		}, 300);
		jQuery("#page").animate({
				marginTop: 0
		}, 300);
		jQuery("body").css({
			overflow: "auto"
		});
	}
	btnFollowUs = false;
});
jQuery(".site-sns-icons .btn-wechat").click(function(){
	jQuery(this).toggleClass("on");
});

jQuery(".site-navigation-mobile > .navigation-bar > .btn-close").click(function(){
	jQuery("body").css({
		overflow: "auto"
	});
	jQuery(".site-menu-mobile > .btn-menu").removeClass("on");
	jQuery(".site-navigation-mobile").fadeOut(300);
});

jQuery(".site-menu-mobile > .btn-menu").click(function(){
	jQuery("body").css({
		overflow: "hidden"
	});
	jQuery(this).addClass("on");

	jQuery(".site-navigation-mobile").fadeIn(500);
});

// Video
jQuery("#videoPosts .videoItem > .overlay .btnShare").click(function(){
	jQuery(this).toggleClass("on");
	if(jQuery(this).find(".textShare").is(":visible")){
		jQuery(this).parent().parent().find(".videoTitle").fadeOut(300);
		jQuery(this).parent().parent().find(".videoShare").fadeIn(300);
		jQuery(this).find(".textShare").fadeOut(0);
		jQuery(this).find(".textClose").fadeIn(0);
	}else{
		jQuery(this).parent().parent().find(".videoTitle").fadeIn(300);
		jQuery(this).parent().parent().find(".videoShare").fadeOut(300);
		jQuery(this).find(".textShare").fadeIn(0);
		jQuery(this).find(".textClose").fadeOut(0);
	}
});

// ipad
jQuery("#videoPosts .videoItem > .btnShare").click(function() {
	jQuery(this).parent().find(".btnPlay, .btnShare").fadeOut(300);
	jQuery(this).parent().find("> .videoShare").fadeIn(300);
});
jQuery("#videoPosts .videoItem > .videoShare .btn-close").click(function() {
	jQuery(this).parent().parent().parent().find(".btnPlay, .btnShare").fadeIn(300);
	jQuery(this).parent().parent().fadeOut(300);
});
jQuery("#videoPosts .videoItem > .btnPlay").click(function() {
	var videoItem = jQuery(this).parent().find(".embedVideo");
	videoItem.html(decodeURIComponent(videoItem.attr("data-src")));

	jQuery(this).parent().find(".embedVideo").fadeIn(0);
	jQuery(this).parent().find(".thImage, .btnPlay, .btnShare").fadeOut(300);
	jQuery(this).parent().find(".overlay").animate({
		bottom: -100
	}, 300);
});

jQuery("#videoPosts .videoItem").find(".videoShare .btn-facebook").click(function(){
	var shareUrl = encodeURIComponent(jQuery(this).parent().attr("data-url"));
	var shareTitle = encodeURIComponent(jQuery(this).parent().attr("data-title"));
	ShareSnsFacebook(shareUrl, shareTitle);
});
jQuery("#videoPosts .videoItem").find(".videoShare .btn-weibo").click(function(){
	var shareUrl = encodeURIComponent(jQuery(this).parent().attr("data-url"));
	var shareTitle = encodeURIComponent(jQuery(this).parent().attr("data-title"));
	var shareImg = encodeURIComponent(jQuery(this).parent().attr("data-pic"));
	ShareSnsWebio(shareUrl, shareTitle, shareImg);
});
jQuery("#videoPosts .videoItem").find(".videoShare .btn-pinterest").click(function(){
	var shareUrl = encodeURIComponent(jQuery(this).parent().attr("data-url"));
	var shareTitle = encodeURIComponent(jQuery(this).parent().attr("data-title"));
	ShareSnsPinterest(shareUrl, shareTitle);
});
jQuery("#videoPosts .videoItem").find(".videoShare .btn-email").click(function(){
	var shareUrl = encodeURIComponent(jQuery(this).parent().attr("data-url"));
	var shareTitle = encodeURIComponent(jQuery(this).parent().attr("data-title"));
	ShareSnsEmail(shareUrl, shareTitle);
});

jQuery("#videoPosts .videoList .videoListItem").click(function(){
	jQuery("#videoPosts .videoItem .embedVideo").html("");
	jQuery("#videoPosts .videoItem").find(".thImage, .btnPlay, .btnShare").fadeIn(0);
	jQuery("#videoPosts .videoItem").find(".overlay").animate({
		bottom: 0
	}, 0);

	jQuery('#videoPosts .videoList .videoListItem').removeClass('on');
	jQuery(this).addClass('on');
	jQuery('#videoPosts .cycle-slideshow').cycle('goto', jQuery(this).index());
});

// Single post
jQuery(".btn-backtotop").click(function(){
	jQuery(window).scrollTo({top:'0', left:'0'}, 500);
});

// Share SNS
function ShareSnsFacebook(url, title){
	window.open("https://www.facebook.com/sharer/sharer.php?t="+title+"&u="+url);
}
function ShareSnsWebio(url, title, pic){
	window.open("http://service.weibo.com/share/share.php?url="+url+"&title="+title+"&pic="+pic);
}
function ShareSnsPinterest(url, title){
	window.open("https://www.pinterest.com/pin/create/button/?url="+url+"&description="+title);
}
function ShareSnsEmail(url, title){
	location.href = "mailto:?subject="+title+"&body="+url;
}